package cn.earas.htmlX.net;

import cn.earas.htmlX.frame.OneFrame;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.*;

public class HttpUtil {

    CloseableHttpClient client = null;

    public HttpUtil() {
        client = Clients.getHttpClient();
    }

    public String getHtml(HttpGet get, String ecode) {
//        OneFrame.showLinks(get.getURI().toString());
        CloseableHttpResponse response = null;
        String string = "";
        try {
            response = client.execute(get);
            HttpEntity entity = response.getEntity();
            string = EntityUtils.toString(entity, ecode);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("获取html失败，链接：" + get.getURI().toString() + "，错误信息：" + e.getMessage());
        } finally {
            try {
                if (response != null)
                    response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return string;
    }

    public String getHtml2(HttpGet get) {
        CloseableHttpResponse response = null;
        String string = "";
        try {
            response = client.execute(get);
            HttpEntity entity = response.getEntity();
            string = EntityUtils.toString(entity, "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null)
                    response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return string;
    }

    public File download(HttpGet get) {
//        OneFrame.showLinks(get.getURI().toString());
        File result = new File(System.currentTimeMillis() + (int) (Math.random() * 900 + 100) + "");
        CloseableHttpResponse response = null;
        HttpEntity entity = null;
        BufferedInputStream in = null;
        BufferedOutputStream out = null;
        try {
            response = client.execute(get);
            if (response.getStatusLine().getStatusCode() != 200) {
                System.out.println("获取文件失败，链接："+get.getURI() + "，状态码：" + response.getStatusLine().getStatusCode());
                return null;
            }
            entity = response.getEntity();
            //读取文件(缓存字节流)
            in = new BufferedInputStream(entity.getContent());
            //写入相应的文件
            out = new BufferedOutputStream(new FileOutputStream(result));
            //读取数据
            //一次性取多少字节
            byte[] bytes = new byte[2048];
            //接受读取的内容(n就代表的相关数据，只不过是数字的形式)
            int n = -1;
            //循环取出数据
            while ((n = in.read(bytes, 0, bytes.length)) != -1) {
                //写入相关文件
                out.write(bytes, 0, n);
            }
            //清楚缓存
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("获取文件失败，链接：" + get.getURI().toString() + "，错误信息：" + e.getMessage());
            result = null;
        } finally {
            //关闭流
            try {
                if (in != null) in.close();
                if (out != null) out.close();
                if (response != null) response.close();
                EntityUtils.consume(entity);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public void close() {
        Clients.putHttpClient(client);
    }
}
